package com.wswy.wyjk.ui.main

import android.arch.lifecycle.Observer
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.raft.framework.base.RFBaseFragment
import com.wswy.wyjk.R
import com.wswy.wyjk.core.App
import com.wswy.wyjk.model.PracticeType
import com.wswy.wyjk.model.SubjectType
import com.wswy.wyjk.ui.EXTRA_DATA
import com.wswy.wyjk.ui.EXTRA_POSITION
import com.wswy.wyjk.ui.common.Router
import com.wswy.wyjk.ui.practiceNew.ExamActivity
import com.wswy.wyjk.ui.practiceNew.ExamRecordActivity
import com.wswy.wyjk.ui.practiceNew.NativeExpressADController
import com.wswy.wyjk.ui.practiceNew.PracticeNewActivity
import com.wswy.wyjk.utils.AdverConfigUtil
import com.wswy.wyjk.utils.Util
import kotlinx.android.synthetic.main.main_fragment_subject_1.*
import timber.log.Timber

class SubjectFragment : RFBaseFragment() {

    companion object {
        fun newInstance(position: Int): SubjectFragment = SubjectFragment().apply {
            arguments = Bundle().apply {
                putInt(EXTRA_POSITION, position)
            }
        }
    }

    private lateinit var subjectHomeViewModel: SubjectHomeViewModel

    private var nativeExpressADController: NativeExpressADController? = null

    override fun getContentLayoutId(): Int {
        return R.layout.main_fragment_subject_1
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        subjectHomeViewModel = provideViewModel(SubjectHomeViewModel::class.java)

        val practiceContext = App.getPracticeContext() ?: kotlin.run {
            finish()
            return
        }

        subjectHomeViewModel.practiceType = PracticeType().apply {

            //获取当前的类型
            licenseType = practiceContext.licenseType
            areaCode = practiceContext.areaCode

            //区分科目和考试类型

            if (practiceContext.haveSubject()) {
                when (arguments?.getInt(EXTRA_POSITION, 0)) {
                    0 -> {
                        subjectType = SubjectType.KEMU1
                    }
                    1 -> {
                        subjectType = SubjectType.KEMU4
                    }
                }

            } else {
                subjectType = SubjectType.CERTIFICATE
            }

        }

        //随机练习
        ll_random_test.setOnClickListener {
//            Router.start(
//                it.context,
//                Router.PRACTICE,
//                practiceType = subjectHomeViewModel.practiceType?.copy()?.apply {
//
//                    type = PracticeType.TYPE_RANDOM
//
//                })
            context!!.startActivity(Intent(context, PracticeNewActivity::class.java).putExtra("type",3))
        }

        ll_remain_test.setOnClickListener {
//            Router.start(
//                it.context,
//                Router.PRACTICE,
//                practiceType = subjectHomeViewModel.practiceType?.copy()?.apply {
//
//                    type = PracticeType.TYPE_UNDONE
//
//                }
            context!!.startActivity(Intent(context, PracticeNewActivity::class.java).putExtra("type",2))
        }

        //开始顺序练习
        fl_test.setOnClickListener {

//            Router.start(
//                it.context,
//                Router.PRACTICE,
//                practiceType = subjectHomeViewModel.practiceType
//            )
            context!!.startActivity(Intent(context, PracticeNewActivity::class.java).putExtra("type",1))
        }

        //章节练习
        ll_chapter_test.setOnClickListener {
            Router.start(it.context, Router.CHAPTER_LIST, Bundle().apply {
                putParcelable(EXTRA_DATA, subjectHomeViewModel.practiceType)
            })
        }

        ll_nanti_test.setOnClickListener {
//            Router.start(
//                it.context,
//                Router.PRACTICE,
//                practiceType = subjectHomeViewModel.practiceType?.copy()?.apply {
//                    type = PracticeType.TYPE_DIFFICULTY
//
//                })
            context!!.startActivity(Intent(context, PracticeNewActivity::class.java).putExtra("type",6))
        }

        //模拟考试
        fl_exam.setOnClickListener {
//            Router.start(
//                it.context,
//                Router.PRACTICE,
//                practiceType = subjectHomeViewModel.practiceType?.copy()?.apply {
//                    type = PracticeType.TYPE_EXAM
//
//                })
            context!!.startActivity(Intent(context, ExamActivity::class.java))
        }

        //考试记录
        ll_exam_record.setOnClickListener {
//            Router.start(it.context, Router.EXAM_RECORD, Bundle().apply {
//                putParcelable(EXTRA_DATA, subjectHomeViewModel.practiceType)
//            })
            context!!.startActivity(Intent(context, ExamRecordActivity::class.java))
        }

        ll_point_rank.setOnClickListener {
           // ToastUtils.showText("该功能即将开放，尽情期待~")
            context!!.startActivity(Intent(context, RankActivity::class.java))
        }


        listenData()

        iv_setting_bottom_logo.setBackgroundResource(
            Util.getMetaDataInt(
                context,
                "setting_downlogo"
            )
        )

        if (!AdverConfigUtil.isShowBanner) {
            ll_banner.visibility = View.GONE
        } else {
            Glide.with(this)
                .load(AdverConfigUtil.homeBannerUrl)
                .into(iv_banner)
        }
        ll_banner.setOnClickListener {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra("type", "")
            startActivity(intent)
        }
        //显示广告
        nativeExpressADController = NativeExpressADController(activity, banner_container,
            AdverConfigUtil.bannerADPOSTID, AdverConfigUtil.isShowBannerAD)
    }

    private fun listenData() {
        subjectHomeViewModel.getCountLiveData().observe(this, Observer {

            it?.run {
                Timber.e("count ----- $completed/$count ")

                tv_count_progress.text = "$completed/$count"

            }
        })
    }

    override fun onResume() {
        super.onResume()
        subjectHomeViewModel.loadCount()
    }

    override fun onDestroy() {
        super.onDestroy()
        if(nativeExpressADController!=null) {
            nativeExpressADController!!.onDestroy()
        }
    }
}